﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MacXponSnBinding
{
    public partial class FormBinding : Form
    {
        private int bindNum = 0;

        public FormBinding()
        {
            InitializeComponent();
        }

        private void textBoxMac_KeyPress(object sender, KeyPressEventArgs e)
        {
            //只能输入16进制数字和回车符号'\r'或者'\n'，MAC地址中的字符只能位0-9,A~F
            if (e.KeyChar == '\n' || e.KeyChar == '\r')
            {
                e.Handled = true;
                if (this.textBoxMac.Text.Trim().Length != 12)
                {
                    AppendRichTextBox("MAC地址不为12位数");
                    this.textBoxMac.Clear();
                    this.textBoxMac.Focus();
                }
                else
                {
                    this.textBoxXponSn.Focus();
                }
            }
            else if ((e.KeyChar >= 'a' && e.KeyChar <= 'f')
                || (e.KeyChar >= 'A' && e.KeyChar <= 'F')
                || (e.KeyChar >= '0' && e.KeyChar <= '9')
                || (e.KeyChar == 8))
            {
                //判断是否位16进制字符，将小写全部转为大写
                e.Handled = false;
            }
            else
            {
                MessageBox.Show("只能输入数字和A-F");
                e.Handled = true;
            }
        }

        

        private void textBoxXponSn_KeyPress(object sender, KeyPressEventArgs e)
        {
            //只能输入16进制数字和回车符号'\r'或者'\n'，MAC地址中的字符只能位0-9,A~F
            if (e.KeyChar == '\n' || e.KeyChar == '\r')
            {
                e.Handled = true;
                //TestStart();
                if (this.textBoxXponSn.Text.Trim().Length != 12)
                {
                    AppendRichTextBox("BOSA_SN地址不为12位数");
                    this.textBoxXponSn.Clear();
                    this.textBoxXponSn.Focus();
                }
                else
                {
                    Binding();
                }
            }
            else if ((e.KeyChar >= 'a' && e.KeyChar <= 'z')
                || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
                || (e.KeyChar >= '0' && e.KeyChar <= '9')
                || (e.KeyChar == 8))
            {
                //判断是否位16进制字符，将小写全部转为大写
                e.Handled = false;
            }
            else
            {
                MessageBox.Show("只能输入数字和A-F");
                e.Handled = true;
            }
        }

        private void Binding()
        {
            SetTextBoxEnable();

            //绑定MAC与SN
            string mac = this.textBoxMac.Text.Trim();
            string xponSn = this.textBoxXponSn.Text.Trim();

            //到数据库中查找是否存在相应的MAC或者xponSn
            Device_Data deviceW = new Device_Data();
            deviceW.SetWhere();
            deviceW.MACAddr = mac;

            var selectDevice = MethodWcf.SelectDeviceDataClient(deviceW);
            if (selectDevice.result == false)
            {
                AppendRichTextBox("未能在数据库中找到相应的MAC地址，绑定失败");
                LockInputMessage("未能在数据库中找到相应的MAC地址，绑定失败");
                SetTextBoxEnable(true);
                MacFocus();
                return;
            }

            //判断XPON的SN是否已经绑定了，如果绑定了则不可以重新绑定
            if (string.IsNullOrEmpty(selectDevice.value[0].LightModuleSN) == false && selectDevice.value[0].LightModuleSN.Length == 12)
            {
                AppendRichTextBox("当前MAC已经绑定了XPON_SN号码，不可以重复绑定");
                LockInputMessage("当前MAC已经绑定了XPON_SN号码，不可以重复绑定");
                SetTextBoxEnable(true);
                MacFocus();
                return;
            }

            //到数据中查找XPON SN是否存在
            BOSA_DATA bosaW = new BOSA_DATA();
            bosaW.SetWhere();
            bosaW.SN = xponSn;
            var selectBosa = MethodWcf.SelectBosaDataCountClient(bosaW);

            if (selectBosa.result == false || selectBosa.message!="1")
            {
                AppendRichTextBox("未能获得BOSA数据，绑定失败");
                LockInputMessage("未能获得BOSA数据，绑定失败");
                SetTextBoxEnable(true);
                MacFocus();
                return;
            }

            //判断此SN是否已经绑定过了
            deviceW.Clear();
            deviceW.SetWhere();

            deviceW.LightModuleSN = xponSn;
            var selectCountDevice = MethodWcf.SelectDeviceDataCountClient(deviceW);
            if (selectCountDevice.result && selectCountDevice.message == "1")
            {
                AppendRichTextBox("该XPON_SN已经绑定过了MAC不可以重复绑定");
                LockInputMessage("该XPON_SN已经绑定过了MAC不可以重复绑定");
                SetTextBoxEnable(true);
                MacFocus();
                return;
            }

            //将SN与MAC地址绑定
            deviceW.Clear();
            deviceW.SetWhere();
            deviceW.MACAddr = mac;
            deviceW.SetUpdate();
            deviceW.LightModuleSN = xponSn;

            var updateDevice = MethodWcf.UpdateDeviceDataClient(deviceW);
            if (updateDevice.result == false)
            {
                AppendRichTextBox(string.Format("绑定失败，错误信息为：{0}", updateDevice.message));
                LockInputMessage(string.Format("绑定失败，错误信息为：{0}", updateDevice.message));
                SetTextBoxEnable(true);
                MacFocus();
                return;
            }

            AppendRichTextBox(string.Format("绑定成功，MAC[{0}]，XponSn[{1}]",mac,xponSn));
            bindNum++;

            ShowBindNum(bindNum);

            SetTextBoxEnable(true);
            MacFocus();
        }

        //锁定窗口，并写入错误信息
        private void LockInputMessage(string msg)
        {
            if (this.labelErrorMsg.InvokeRequired)
            {
                Action<string> d = new Action<string>(LockInputMessage);
                Invoke(d, msg);
            }
            else
            {
                this.groupBoxBind.Enabled = false;
                this.groupBoxUnBind.Enabled = false;
                this.labelErrorMsg.Text = msg;
            }
        }

        private void richTextBoxMsg_TextChanged(object sender, EventArgs e)
        {
            richTextBoxMsg.SelectionStart = richTextBoxMsg.Text.Length;
            richTextBoxMsg.ScrollToCaret();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            string mac = this.textBoxMacTwo.Text.Trim();
            if (mac.Length != 12)
            {
                AppendRichTextBox("MAC地址不为12位");
                return;
            }

            Device_Data deviceW = new Device_Data();
            deviceW.SetWhere();
            deviceW.MACAddr = mac;
            deviceW.SetUpdate();
            deviceW.LightModuleSN = "";

            var updateDevice = MethodWcf.UpdateDeviceDataClient(deviceW);
            if (updateDevice.result == false)
            {
                AppendRichTextBox("解除绑定失败");
                return;
            }

            AppendRichTextBox("解除绑定成功");

            MacTwoFocus();
        }

        //将信息检查信息窗口
        private void AppendRichTextBox(string value)
        {
            if (this.richTextBoxMsg.InvokeRequired)
            {
                Action<string> d = new Action<string>(AppendRichTextBox);
                Invoke(d, new object[] { value });
            }
            else
            {
                if (this.richTextBoxMsg.TextLength >= 5000)
                    this.richTextBoxMsg.Clear();
                this.richTextBoxMsg.AppendText(value);
                this.richTextBoxMsg.AppendText("\n");
            }
        }

        private void ShowBindNum(int bindNum)
        {
            if (this.labelBindingNum.InvokeRequired)
            {
                Action<int> d = new Action<int>(ShowBindNum);
                Invoke(d, bindNum);
            }
            else
            {
                this.labelBindingNum.Text = bindNum.ToString();
            }
        }

        private void MacFocus()
        {
            if (this.textBoxMac.InvokeRequired)
            {
                Action d = new Action(MacFocus);
                Invoke(d);
            }
            else
            {
                this.textBoxMac.Clear();
                this.textBoxXponSn.Clear();
                this.textBoxMac.Focus();
            }
        }

        private void SetTextBoxEnable(bool enable = false)
        {
            if (this.textBoxMac.InvokeRequired)
            {
                Action<bool> d = new Action<bool>(SetTextBoxEnable);
                Invoke(d, enable);
            }
            else
            {
                this.textBoxMac.Enabled = enable;
                this.textBoxXponSn.Enabled = enable;
            }
        }

        private void MacTwoFocus()
        {
            if (textBoxMacTwo.InvokeRequired)
            {
                Action d = new Action(MacTwoFocus);
                Invoke(d);
            }
            else
            {
                this.textBoxMacTwo.Clear();
                this.textBoxMacTwo.Focus();
            }
        }

        private void buttonUnLock_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\n' || e.KeyChar == '\r')
            {
                e.Handled = true;
            }
            if (e.KeyChar == ' ')
            {
                this.groupBoxBind.Enabled = true;
                this.groupBoxUnBind.Enabled = true;
                this.labelErrorMsg.Text = null;
                this.MacFocus();
            }
        }
    }
}
